import numpy as np
import random


def array_to_matrix(A, m, n, col_major, dtype=np.float16):
    res = []
    if col_major:
        for r in range(m):
            row = []
            for c in range(n):
                row.append(A[c * m + r])
            res.append(row)
    else:
        for r in range(m):
            row = []
            for c in range(n):
                row.append(A[r * m + c])
            res.append(row)
    return np.array(res).astype(dtype)


def gdb_to_data(filename):
    with open(filename, "r") as f:
        elements = []
        for line in f:
            elements.append(
                [
                    eval(i)
                    for i in (
                        line.strip("\n")
                        .replace(" ", "")
                        .split("=")[1]
                        .replace("}", "")
                        .replace("{", "")
                        .split(",")
                    )
                ]
            )
    return elements


# elements = gdb_to_data("gdb.txt")
# A = np.array(elements[0]).astype(np.float32).reshape(16, 32)
# B = np.array(elements[1]).astype(np.float32).reshape(32, 16)
# if len(elements) == 3:
#     C = np.array(elements[2]).astype(np.float32).reshape(16, 16)
# np_c = np.matmul(A, B)
# A_col_major = array_to_matrix(elements[0], 16, 32, True, np.float32)
# B_col_major = array_to_matrix(elements[1], 32, 16, True, np.float32)
# C_col_major = np.matmul(A_col_major, B_col_major)
# diff = np_c - C_col_major
# # print(np_c.shape, C.shape)
# print(np.matmul(A_col_major[4:8, :16], B_col_major[:16, 4:8]))
m = 4
k = 4
n = 3
random.seed(0)
A = [i for i in range(1, m * k + 1)]
B = [random.randint(0, k * n) for i in range(k * n)]
print(np.matmul(np.array(A).reshape((m, k)), np.array(B).reshape(k, n)))
print(A, B)
